package ysq.xyg.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import ysq.xyg.model.Activity;
import ysq.xyg.model.Order;
import ysq.xyg.model.info.OrderInfo;
import ysq.xyg.model.info.Page;
import ysq.xyg.model.info.ReturnInfo;
import ysq.xyg.service.ActivityService;
import ysq.xyg.service.OrderService;
import ysq.xyg.util.BaseConstant;
import ysq.xyg.util.BaseUtil;
import ysq.xyg.util.DateUtil;
import ysq.xyg.util.EasyPoiUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.util.Date;
import java.util.List;

/**
 * 文章管理
 */
@Controller
@RequestMapping("/order")
public class OrderController extends BaseController {

    private static Logger log= LoggerFactory.getLogger(OrderController.class);
    @Resource
    private ActivityService activityService;
    @Resource
    private OrderService orderService;

    private Order order;

    @RequestMapping("/listPage")
    public ModelAndView List(){
        return new ModelAndView("/orderList");
    }

    @RequestMapping(value = "/jsonList",method = RequestMethod.POST)
    public @ResponseBody JSONObject jsonList(Order order,Page page) throws MalformedURLException, UnsupportedEncodingException {
        PageInfo<Order> pageInfo = orderService.pageInfoLeft(order, page.getLimit(), page.getPage());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 0);
        jsonObject.put("msg", "");
        jsonObject.put("count", pageInfo.getTotal());
        if (pageInfo != null && pageInfo.getList() != null && pageInfo.getList().size() > 0) {
            jsonObject.put("data",pageInfo.getList());
        }
        return jsonObject;
    }

    @RequestMapping(value = "/selectEntity",method = RequestMethod.GET)
    public ModelAndView selectEntity(String id,String type){
        ModelAndView view = new ModelAndView("/orderUpdateOrSave");
        order = orderService.selectByPrimaryKey(id);
        view.addObject("order", order);
        view.addObject("type", type);
        return view;
    }

    @RequestMapping(value = "/deleteEntity",method = RequestMethod.GET)
    public @ResponseBody JSONObject deleteEntity(String id){
        ReturnInfo returnInfo=new ReturnInfo();
        JSONObject jsonObject=new JSONObject();
        if(StringUtil.isEmpty(id)){
            returnInfo=new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_CANSHU);
        }else{
           int ids= orderService.deleteByPrimaryKey(id);
            if(ids <= 0){
                returnInfo = new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_DELETE);
            }
        }
        jsonObject.put("returnInfo",returnInfo);
        return jsonObject;
    }
    @RequestMapping(value = "/export",method = RequestMethod.POST)
    public void export(HttpServletResponse response, Order order){
        String title="活动参与人员信息";
        List<Order> list=orderService.listLeft(order);
        // 导出操作
        EasyPoiUtil.exportExcel(list, title, "人员信息", Order.class, title+".xls", response);

    }
}
